/**
 * Copyright(c) Hainan eKing Technology co., LTD
 *
 * @date 2013-9-30
 * 	
 * Original Author: 郑兴(zhengxing)
 *
 * ChangeLog
 *
 */
package com.hnas.sys.core.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.util.AntPathRequestMatcher;
import org.springframework.security.web.util.RequestMatcher;

import com.hnas.sys.domain.ResourceStatus;
import com.hnas.sys.orm.AuthorityResource;
import com.hnas.sys.orm.Resource;
import com.hnas.sys.service.system.dto.DTOAuthority;
import com.hnas.sys.service.system.dto.DTOMenuPackage;
import com.hnas.sys.service.system.dto.DTOResource;

/**
 * <pre>缓存工具类</pre>
 * @author 郑兴(zhengxing)
 *
 */
public class CacheUtil {
	private static final Cache cache = CacheManager.getInstance().getCache("systemCache");
	
	/**
	 * <pre>设置资源缓存</pre>
	 * @Auther 郑兴(zhengxing)
	 * @param resources
	 */
	public static void setResourceCache(List<DTOResource> resources) {
		cache.put(new Element("resources", resources));
		Map<Long, DTOResource> resourceMap = new HashMap<Long, DTOResource>();
		for(DTOResource resource : resources) {
			resourceMap.put(resource.getId(), resource);
		}
		cache.put(new Element("resourcesMap", resourceMap));
	}
	
	/**
	 * <pre>设置菜单包缓存</pre>
	 * @Auther 郑兴(zhengxing)
	 * @param menuPackages
	 */
	public static void setMenuPackageCache(List<DTOMenuPackage> menuPackages) {
		cache.put(new Element("menuPackages", menuPackages));
		Map<Long, DTOMenuPackage> mpMap = new HashMap<Long, DTOMenuPackage>();
		for(DTOMenuPackage dmp : menuPackages) {
			mpMap.put(dmp.getId(), dmp);
		}
		cache.put(new Element("menuPackageMap", mpMap));
	}
	
	/**
	 * <pre>设置权限缓存</pre>
	 * @Auther 郑兴(zhengxing)
	 * @param authorities
	 */
	public static void setAuthorityCache(List<DTOAuthority> authorities) {
		cache.put(new Element("authorities", authorities));
		setAuthorityStatusMap(authorities);
	}
	
	/**
	 * <pre>从缓存获取资源集合，如果缓存中没有资源数据，则返回空集合</pre>
	 * @Auther 郑兴(zhengxing)
	 * @return
	 */
	public static List<DTOResource> getResourceCache() {
		if(null != cache.get("resources")) {
			return (List<DTOResource>) cache.get("resources").getObjectValue();
		}
		return new ArrayList<DTOResource>();
	}
	
	/**
	 * <pre>获取资源Map</pre>
	 * @Auther 郑兴(zhengxing)
	 * @return
	 */
	public static Map<Long, DTOResource> getResourcesMapCache() {
		if(null != cache.get("resourcesMap")) {
			return (Map<Long, DTOResource>) cache.get("resourcesMap").getObjectValue();
		}
		return new HashMap<Long, DTOResource>();
	}
	
	/**
	 * <pre>从缓存获取菜单包集合，如果缓存中没有菜单包数据，则返回空集合</pre>
	 * @Auther 郑兴(zhengxing)
	 * @return
	 */
	public static List<DTOMenuPackage> getMenuPackageCache() {
		if(null != cache.get("menuPackages")) {
			return (List<DTOMenuPackage>) cache.get("menuPackages").getObjectValue();
		}
		return new ArrayList<DTOMenuPackage>();
	}
	
	/**
	 * <pre>获取菜单包Map</pre>
	 * @Auther 郑兴(zhengxing)
	 * @return
	 */
	public static Map<Long, DTOMenuPackage> getMenuPackageMapCache() {
		if(null != cache.get("menuPackageMap")) {
			return (Map<Long, DTOMenuPackage>) cache.get("menuPackageMap").getObjectValue();
		}
		return new HashMap<Long, DTOMenuPackage>();
	}
	
	/**
	 * <pre>获取权限集合缓存，如果缓存中没有权限数据，则返回空集合</pre>
	 * @Auther 郑兴(zhengxing)
	 * @return
	 */
	public static List<DTOAuthority> getAuthorityCache() {
		if(null != cache.get("authorities")) {
			return (List<DTOAuthority>) cache.get("authorities").getObjectValue();
		}
		return new ArrayList<DTOAuthority>();
	}
	
	/**
	 * <pre>获取权限状态</pre>
	 * @Auther 郑兴(zhengxing)
	 * @return
	 */
	public static Map<String, ResourceStatus> getAuthorityStatusMap() {
		if(null != cache.get("authorityStatus")) {
			return (Map<String, ResourceStatus>) cache.get("authorityStatus").getObjectValue();
		}
		return new HashMap<String, ResourceStatus>();
	}
	
	/**
	 * <pre>设置权限状态</pre>
	 * @Auther 郑兴(zhengxing)
	 * @param authorities
	 */
	public static void setAuthorityStatusMap(List<DTOAuthority> authorities) {
		Map<String, ResourceStatus> authorityStatus = new HashMap<String, ResourceStatus>();
		for(DTOAuthority authority : authorities) {
			authorityStatus.put("ROLE_" + authority.getId(), authority.getStatus());
		}
		cache.put(new Element("authorityStatus", authorityStatus));
	}
	
	public static void setMetaDataCache(List<Resource> resources) {
		LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>> requestMap = new LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>>();
		for(Resource resource : resources) {
			RequestMatcher key = new AntPathRequestMatcher(resource.getResource()+"**");
			Collection<ConfigAttribute> configAttributes = new LinkedList<ConfigAttribute>();
			for(final AuthorityResource ar : resource.getAuthorityResources()) {
				configAttributes.add(new SecurityConfig("ROLE_" + ar.getAuthority().getId()));
			}
			requestMap.put(key, configAttributes);
		}
		cache.put(new Element("metaData", requestMap));
	}
	
	public static LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>> getMetaData() {
		if(null != cache.get("metaData")) {
			return (LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>>) cache.get("metaData").getObjectValue();
		}
		return new LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>>();
	}
}
